package com.dingli.servlet.ajax;

import com.dingli.DAO.queryTicketDAO;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.*;

@WebServlet(urlPatterns = "/queryTicket")
public class QueryTicketServlet extends HttpServlet {
    static queryTicketDAO queryTicketDAO = new queryTicketDAO();
    static String selectType = "";
    static int cur = 1, step = 5;
    static int size = queryTicketDAO.queryCount();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");

        //下拉框选择查询 {车号, 两站, 起点}
        if(!(type == null || type.equals(""))){
            if(type.equals("all")){
                List<Map<String, Object>> rawTickets = queryTicketDAO.selectAllticket();
                ObjectMapper om = new ObjectMapper();
                String json = om.writeValueAsString(rawTickets);
                resp.getWriter().write(json);
                return;
            } else if(type.equals("getCurPage")){
                resp.getWriter().write(new ObjectMapper().writeValueAsString(cur));
                return;
            }
            selection(type,req,resp);
        }
        //分页
        String transType = req.getParameter("transType");
        if(!(transType == null || transType.equals(""))){
            transferTo(transType,req,resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    public void transferTo(String type, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        if(type.equals("first")){ cur = 1; }
        else if(type.equals("last")){ cur = size; }
        else if(type.equals("next")) { cur++; }
        else if(type.equals("pre")){ cur--; }
        else {
            resp.getWriter().write("null");
            return;
        }
        if(cur > size) cur = size;
        if(cur < 1) cur = 1;
        selection(selectType, req, resp);
    }

    public void selection(String type, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        selectType = type;
        if (type.equals("0")) {
            selectLineName(cur, req, resp);
        } else if (type.equals("1") || type.equals("2")) {
            selectLineCites(cur, req, resp);
        } else resp.getWriter().write("null");
    }

    public void selectLineName(int pageNum, HttpServletRequest req, HttpServletResponse resp){
        try {
            String name = req.getParameter("line");//可能为空
            if(name == null) name = "";
            List<Map<String, Object>> rawTickets = queryTicketDAO.queryTicketByName(pageNum, step, name);
            resp.getWriter().write(new ObjectMapper().writeValueAsString(rawTickets));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void selectLineCites(int n, HttpServletRequest req, HttpServletResponse resp){
        try {
            String from = req.getParameter("from");
            String to = req.getParameter("to");
            if(to == null) to = "";
            List<Map<String, Object>> rawTickets = queryTicketDAO.queryTicketByCite(n, step, from, to);
            resp.getWriter().write(new ObjectMapper().writeValueAsString(rawTickets));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

